home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / comm / bbs / citsrc6K05.lha / oldvortex.c < prev    next >
C/C++ Source or Header  |  1996-10-29  |  8KB  |  280 lines

  1. /**
  2.                                vortex.c
  3.  
  4.  Network "vortex" (aka infinite loop) handling code.
  5.  
  6.  
  7. **/
  8. #include "ctdl.h"
  9. #include "math.h"
  10. #include "vortex.h"
  11. #include <dos.h>
  12. /**
  13.                                contents
  14.        InitVortexing()         allocate & initialize buffers
  15.        NotVortex()             checks for vortex possibility
  16. **/
  17.  
  18.  
  19. /**
  20.    External variable definitions
  21. **/
  22. extern CONFIG    cfg;   /* Lots an lots of variables    */
  23. extern MessageBuffer   msgBuf;
  24. extern FILE      *netLog;
  25. extern char *READ_ANY, *APPEND_ANY;
  26. extern char *R_W_ANY, *WRITE_ANY;
  27. extern char logNetResults;
  28. extern char netDebug;
  29.  
  30. /**
  31.  -Vortex Handling-
  32.  
  33.     Vortex: the phenomenon of messages showing up on other systems more than
  34.  once.  The cause is messages being sent from one system to another and
  35.  eventually getting back to the originating system.  This could be in two
  36.  flavors:
  37.  
  38.     A -- B -- C -- eventually back to A
  39.  
  40.     or
  41.  
  42.     A -- B -- C -- D -- eventually back to B or C...
  43.  
  44.     See vortex.h for a complete description of the data records.
  45. **/
  46. char VortexHandle = TRUE;   /* default to Vortex Handling */
  47. SYS_FILE vareaname;         /* set to #netarea plus vortex */
  48. SYS_FILE vfilename;         /* full filename of current vortex file */
  49. static struct elist *head;  /* error list */
  50. void Fix_Name(char *out, char *in);
  51. static ROOM_ENTRY_TYPE *Vortex_Find_Node(label nodeid, label room );
  52.  
  53. void Fix_Name(char *out, char *in)
  54.   {
  55.   /**
  56.     take the input name and remove all spaces and special characters
  57.     can be used to append one "normalized" string onto another.
  58.   **/
  59.   char i,j;
  60.   j = 0;
  61.   for(i=0; i<strlen(in); i++)
  62.     {
  63.     if( !isalnum(in[i]) )continue;
  64.     out[j++] = in[i];
  65.     };
  66.   out[j] = '\0';
  67.   }
  68.  
  69. static ROOM_ENTRY_TYPE *Vortex_Find_Node(label nodeid, label room )
  70.   {
  71.   FILE *vfd;
  72.   ROOM_ENTRY_TYPE *vptr;
  73.   int  j;
  74.   strcpy(vfilename,vareaname);
  75.   j = strlen(vfilename);
  76.   vfilename[j++] = '/';
  77.   Fix_Name(&vfilename[j],nodeid);
  78.   j = strlen(vfilename);
  79.   vfilename[j++] = '.';
  80.   Fix_Name(&vfilename[j], room);
  81.   j = strlen(vfilename);
  82.   vfilename[j] = '\0';
  83.   vptr = (ROOM_ENTRY_TYPE *)malloc(sizeof(ROOM_ENTRY_TYPE));
  84.   if( vptr == NULL ) return NULL;
  85.   if( (vfd = fopen(vfilename, READ_ANY) ) == NULL )
  86.     {
  87.     /**
  88.       assume it does not exist... create it.
  89.     **/
  90.     memset(vptr, '\0', sizeof(ROOM_ENTRY_TYPE));  /* initialize it */
  91.     }
  92.   else
  93.     {
  94.     /**
  95.       read the entries
  96.     **/
  97.     if( fread(vptr, sizeof(ROOM_ENTRY_TYPE), 1, vfd) != 1 )
  98.       {
  99.       splitF(netLog, "System error with reading %s\n",vfilename);
  100.       free(vptr);
  101.       vptr = NULL;
  102.       };
  103.     fclose(vfd);
  104.     };
  105.   return vptr;
  106.   }
  107.  
  108. /**
  109.  VortexInit()
  110.  
  111.  This function initializes vortexing.  Currently, that means making sure
  112.  that the directory in the #NETAREA called "vortex" exists, it will be created
  113.  if not.  Otherwise, nothing is done at this point.
  114. **/
  115.  
  116. void VortexInit()
  117.   {
  118.   /**
  119.      Initialize the system for vortex checking
  120.      1) create vortex directory if none exits
  121.   **/
  122.   if (!VortexHandle) return ;   /* exit if not doing vortex checking */
  123.   makeSysName(vareaname, "vortex", &cfg.netArea);
  124.   if( access(vareaname, F_OK) != 0 )
  125.     {
  126.     /**
  127.       does not exist, we must create the directory
  128.     **/
  129.     if (mkdir(vareaname) != 0)
  130.       mPrintf("Error creating %s!\n ",vareaname);
  131.     };
  132.   return;
  133.   }
  134.  
  135. /**
  136.  InitVortexing()
  137.  
  138.  This is an initialization function which should be called before each
  139.  session of checking messages in from a network session.
  140.  
  141.  Initialize the error list.
  142. **/
  143. void InitVortexing()
  144.   {
  145.   head = NULL;    /* should already be empty */
  146.   return;         /* nothing else to do */
  147.   }
  148.  
  149. /**
  150.  NotVortex()
  151.  
  152.  This checks to see if the msg in msgBuf is vortexing or not.  FALSE is
  153.  returned if the message should be discarded.  TRUE is not a vortex.
  154.  
  155.  All vortex files are created and updated during this call.
  156.  
  157. **/
  158.  
  159. char NotVortex()
  160.   {
  161.   int i;
  162.   char vortex_flag = TRUE;
  163.   ROOM_ENTRY_TYPE *node;  /* the current node */
  164.   char mynode[20], yournode[20];
  165.  
  166.   /**
  167.     First, we must find the Node Entry that matches the Message
  168.     The function Vortex_Find_Node() will always return a ROOM_ENTRY
  169.     pointer, it will create it if it does not exits.
  170.   **/
  171.   if (!VortexHandle) return TRUE;   /* exit if not doing vortex checking */
  172.   Fix_Name(  mynode, &cfg.codeBuf[cfg.nodeName]);
  173.   Fix_Name(yournode, msgBuf.mboname);
  174.   if( strcmp(mynode, yournode) == 0 )
  175.     {
  176.     splitF(netLog, " NotVortex: detected messages from %s\n",msgBuf.mboname);
  177.     splitF(netLog, "            Routed from %s\n",msgBuf.mborig);
  178.     return FALSE;
  179.     };
  180.   if( ( node = Vortex_Find_Node(msgBuf.mborig, msgBuf.mbroom) ) == NULL )
  181.     {
  182.     splitF(netLog, " NotVortex: node (%s) bad or no memory\n",msgBuf.mboname);
  183.     return TRUE;
  184.     };
  185.   if( node->index == 0 )
  186.     {
  187.     if( logNetResults)
  188.       {
  189.       splitF(netLog, "Msg from unmonitored node %s routed from %s, adding it.\n"
  190.       , msgBuf.mboname, msgBuf.mborig);
  191.       };
  192.     }
  193.   else  /* check to see if already in list */
  194.     {
  195.     for( i=0; i < node->index && vortex_flag; i++)
  196.       {
  197.       struct elist *error;
  198.       MESSAGE_ENTRY_TYPE *mptr = &node->msg_entry[i];
  199.       if( strcmp(mptr->mbsrcId, msgBuf.mbsrcId) != 0 ) continue;
  200.       if( strcmp(mptr->mbdate,  msgBuf.mbdate ) != 0 ) continue;
  201.       if( strcmp(mptr->mbtime,  msgBuf.mbtime ) != 0 ) continue;
  202.       vortex_flag = FALSE;
  203.       error = head;
  204.       while( error )
  205.         {
  206.         if( strcmp(error->room,  msgBuf.mbroom)   == 0 &&
  207.             strcmp(error->nodeid, msgBuf.mborig)  == 0 ) break;
  208.         error = error->next;
  209.         };
  210.       if( error == NULL )
  211.         {
  212.         error = ( struct elist *)malloc(sizeof(struct elist));
  213.         if( error == NULL )
  214.           {
  215.           mPrintf(" Error: No memory for vortex error list\n");
  216.           }
  217.         else
  218.           {
  219.           strcpy(error->room,  msgBuf.mbroom);
  220.           strcpy(error->nodeid, msgBuf.mborig);
  221.           error->next = head;
  222.           head        = error;
  223.           };
  224.         };
  225.       };
  226.     };
  227.   if( vortex_flag == TRUE )
  228.     {
  229.     FILE *vfd;
  230.     MESSAGE_ENTRY_TYPE *mptr = &node->msg_entry[node->next_slot];
  231.     strcpy(mptr->mbsrcId, msgBuf.mbsrcId);
  232.     strcpy(mptr->mbdate,  msgBuf.mbdate );
  233.     strcpy(mptr->mbtime,  msgBuf.mbtime );
  234.     node->index++;
  235.     if( node->index > MAX_VORTEX_SIZE) node->index = MAX_VORTEX_SIZE;
  236.     node->next_slot = ( node->next_slot + 1 ) % MAX_VORTEX_SIZE;
  237.     if( (vfd = fopen(vfilename, WRITE_ANY) ) == NULL )
  238.       {
  239.       mPrintf("System error with open for write of %s\n",vfilename);
  240.       }
  241.     else
  242.       {
  243.       if( fwrite(node, sizeof(ROOM_ENTRY_TYPE), 1, vfd) != 1 )
  244.         {
  245.         mPrintf("System error with write of %s\n",vfilename);
  246.         };
  247.       fclose(vfd);
  248.       };
  249.     };
  250.   free(node);
  251.   return vortex_flag;
  252.   }
  253.  
  254. /**
  255.  FinVortexing()
  256.  
  257.  This function should be called to finish a vortex checking session.
  258.  Nothing to do in this function right now.
  259. **/
  260. void FinVortexing()
  261.   {
  262.   struct elist *error;
  263.   if (!VortexHandle)  return;
  264.   if ( head == NULL ) return;
  265.   strcpy(msgBuf.mbtext, "Vortex attempted involving the following system(s): \n");
  266.   error = head;
  267.   head  = error->next;
  268.   while( error && strlen(msgBuf.mbtext) < 7000  )
  269.     {
  270.     strcat(msgBuf.mbtext, " Node Id: ");
  271.     strcat(msgBuf.mbtext, error->nodeid);
  272.     strcat(msgBuf.mbtext, " Room: ");
  273.     strcat(msgBuf.mbtext, error->room);
  274.     strcat(msgBuf.mbtext, "\n");
  275.     free(error);
  276.     error = head;
  277.     if( error ) head = error->next;
  278.     };
  279.   }
  280.